<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/CategoryDomain.php';
class CategoryDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, CategoryDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO category(categoryID,code,name,imagePath,parent,level,type,display)
					VALUES	(
								".$obj->getCategoryID().",
								'".$obj->getCode()."',
								'".$obj->getName()."',
								'".$obj->getImagePath()."',
								".$obj->getParent().",
								".$obj->getLevel().",
								".$obj->getType().",
								".$obj->getDisplay()."
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM category WHERE categoryID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE category
				SET
					category.code = '".$obj->getCode()."',
					category.name = '".$obj->getName()."',
					category.imagePath = '".$obj->getImagePath()."',
					category.parent = ".$obj->getParent().",
					category.level = ".$obj->getLevel().",
					category.type = ".$obj->getType().",
					category.display = ".$obj->getDisplay()."
				 WHERE categoryID=".$obj->getCategoryID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$categoryDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM category WHERE category.categoryID = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$categoryDomain = new CategoryDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $categoryDomain;
	}

	public function findAll()
	{
		$categoryDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM category";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$categoryDomain = new CategoryDomain($data);
				$categoryDomainArr[] = $categoryDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $categoryDomain;
	}

}
?>